import 'package:flutter/material.dart';

enum Option { A, B, C }

class DialogDemo extends StatefulWidget {
  @override
  _DialogDemoState createState() => _DialogDemoState();
}

class _DialogDemoState extends State<DialogDemo> {
  bool _isExpanded = false;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Text("dialogDemo"),
        OutlinedButton(
          onPressed: () => _openSimpleDialog(),
          child: Text("显示simpleDialog"),
        ),
        ElevatedButton(
          onPressed: () => _openAlertDialog(),
          child: Text('显示alertDialog'),
        ),
        ElevatedButton(
          onPressed: () => _openBottomSheet(),
          child: Text('显示bottomSheet'),
        ),
        ElevatedButton(
          onPressed: () => _openModalBottomSheet(),
          child: Text('显示ModalBottomSheet'),
        ),
        ExpansionPanelList(
          materialGapSize: 100,
          expansionCallback: (index,expand) {
            setState(() {
              _isExpanded=expand;
            });
          },
          children: [
            ExpansionPanel(
              canTapOnHeader: true, // 允许点击整个头部区域
              headerBuilder: (context, isExpand) {
                return Container(
                  padding: EdgeInsets.all(10),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text("Panel Title"),
                      isExpand
                          ? Icon(Icons.keyboard_arrow_up, color: Colors.green)
                          : Icon(Icons.keyboard_arrow_down, color: Colors.grey),
                    ],
                  ),
                );
              },
              body: Text("收缩面板"),
              isExpanded: _isExpanded,
            ),
            ExpansionPanel(
              headerBuilder: (context, isExpand) {
                return Container(
                  padding: EdgeInsets.all(10),
                  child: Text("Panel A"),
                );
              },
              body: Text("收缩面板"),
              isExpanded: _isExpanded,
            ),
            ExpansionPanel(
              headerBuilder: (context, isExpand) {
                return ListTile(
                  title: Text("Panel C"),
                  trailing: Icon(isExpand ? Icons.abc : Icons.chevron_right),
                );
              },
              body: Text("收缩面板"),
              isExpanded: _isExpanded,
            ),
          ],
        ),
      ],
    );
  }

  Future<void> _openSimpleDialog() async {
    final result = await showDialog(
      context: context,
      builder: (BuildContext context) {
        return SimpleDialog(
          backgroundColor: Colors.pink,
          titlePadding: EdgeInsets.zero,
          title: Text("标题"),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.zero, // 去掉圆角
          ),
          children: [
            SimpleDialogOption(child: Text("Option A"), onPressed: _onDimiss),
            SimpleDialogOption(child: Text("Option B"), onPressed: _onDimiss),
            SimpleDialogOption(child: Text("Option C"), onPressed: _onDimiss),
            SimpleDialogOption(child: Text("Option D"), onPressed: _onDimiss),
          ],
        );
      },
    );
    switch (result) {
      case Option.A:
        {
          debugPrint("simpleDialog Options A");
          break;
        }
      case Option.B:
        {
          debugPrint("simpleDialog Options B");
        }
    }
  }

  void _onDimiss() {
    Navigator.of(context).pop(Option.A);
  }

  void _openAlertDialog() {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text("我是title"),
          content: Text("我是内容"),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.of(context).pop(-1);
              },
              child: Text("cancel"),
            ),
            TextButton(
              onPressed: () {
                Navigator.of(context).pop(1);
              },
              child: Text("ok"),
            ),
          ],
        );
      },
    );
  }

  void _openBottomSheet() {
    Scaffold.of(context).showBottomSheet((context) {
      return Container(
        width: double.infinity,
        height: 200,
        color: Colors.pink,
        child: Text("1100"),
      );
    }, showDragHandle: true);
  }

  void _openModalBottomSheet() {
    showModalBottomSheet(
      context: context,
      routeSettings: RouteSettings(),
      builder: (BuildContext context) {
        return Container(
          width: double.infinity,
          height: 200,
          color: Colors.pink,
          child: Text("1100"),
        );
      },
    );
  }
}
